package org.example.demo1.servlets.LoginPage;





import com.google.gson.Gson;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.example.demo1.Entity.User;
import org.example.demo1.utils.ChineseIdentifierUtils;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import static org.example.demo1.utils.ChineseIdentifierUtils.identify;
import static org.example.demo1.utils.InquireTools.*;

@WebServlet("/API/resetPassword")
public class ResetPasswordServlet extends HttpServlet {


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String EmailOrPhone = req.getParameter("EmailOrPhone");
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");
        User user = null;
        Map<String, Object> data = new HashMap<>();
        user=getUserInformation(username);
        if(user==null){
            data.put("status", "error");
            data.put("code", 401);
            data.put("message", "用户名不存在");
            String json = new Gson().toJson(data);
            // 发送响应
            PrintWriter out = resp.getWriter();
            out.print(json);
            out.flush();
            return;
        }
        ChineseIdentifierUtils.IdentifierType type = identify(EmailOrPhone);
        if (type == ChineseIdentifierUtils.IdentifierType.EMAIL) {
            user=getUserInformationByEmailAndUsername(username,EmailOrPhone);
        }
        if (type == ChineseIdentifierUtils.IdentifierType.PHONE) {
            user=getUserInformationByPhoneAndUsername(username,EmailOrPhone);
        }

        if(user==null){
            data.put("status", "error");
            data.put("code", 402);
            data.put("message", "输入的邮件或手机号有误");
            String json = new Gson().toJson(data);
            // 发送响应
            PrintWriter out = resp.getWriter();
            out.print(json);
            out.flush();
            return;
        }

        if (updateUserInformation(username,password)) {
            data.put("status", "success");
            data.put("code", 200);
            data.put("message", "密码重置成功");
            String json = new Gson().toJson(data);
            // 发送响应
            PrintWriter out = resp.getWriter();
            out.print(json);
            out.flush();
            return;
        }else{
            data.put("status", "error");
            data.put("code", 403);
            data.put("message", "密码更新失败");
            String json = new Gson().toJson(data);
            // 发送响应
            PrintWriter out = resp.getWriter();
            out.print(json);
            out.flush();
            return;
        }
    }
}